#include "movecircleform.h"
#include "ui_movecircleform.h"
#include "data.h"
#include <QThread>
#include<QtDebug>
#include<QString>
#include <QTimer>
#include<QMessageBox>

// int axislist[2]={0,1};
// bool runflage=false;


MoveCircleForm::MoveCircleForm(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MoveCircleForm)
{
    ui->setupUi(this);


    connect(ui->pushButton_start,&QPushButton::clicked,this,[=](){on_Button_start_clicked();});
    connect(ui->pushButton_stop,&QPushButton::clicked,this,[=](){on_PushButton_stop_clicked();});
    connect(ui->pushButton_cleanpos,&QPushButton::clicked,this,[=](){on_PushButton_cleanpos_clicked();});

    QTimer *timer1=new QTimer(this);
    connect(timer1,&QTimer::timeout,this,&MoveCircleForm::updateWin);
    timer1->start(100);


}

MoveCircleForm::~MoveCircleForm()
{
    delete ui;
}

//启动
void MoveCircleForm::on_Button_start_clicked(){
    if(Data::handle!=nullptr){
        float speed=ui->spinBox_sp->value();
        float accel=ui->spinBox_acc->value();
        float decel=ui->spinBox_del->value();
        float xpoint=ui->spinBox_xpos->value();
        float ypoint=ui->spinBox_ypos->value();
        float xpointmid=ui->spinBox_xposmid->value();
        float ypointmid=ui->spinBox_yposmid->value();
        int aspect=ui->radioButton_1->isChecked()?0:1;
        float radio=ui->spinBox_radio->value();

        if(xpoint<=xpointmid||ypoint<=ypointmid){
            warn=QMessageBox::information(this,tr("warn"),"圆弧中点要小于运动中点");
            return;
        }


        PMC_Base(Data::handle,4,axislist);
        for(int i=0;i<4;++i){
            int enable=0;
            PMC_GetAxisEnable(Data::handle,axislist[i],&enable);
            if(enable==0){
                QMessageBox::about(NULL,"Warn","请检查AXIS0-3的使能情况！！");
                return;
            }
            PMC_SetAtype(Data::handle,axislist[i],1);
            PMC_SetUnits(Data::handle,axislist[i],200);
            PMC_SetSpeed(Data::handle,axislist[i],speed);
            PMC_SetAccel(Data::handle,axislist[i],accel);
            PMC_SetDecel(Data::handle,axislist[i],decel);
        }

        ui->pushButton_start->setEnabled(false);
        ui->label_flag->setText("运行");
        ui->label_flag->setStyleSheet("background-color: rgb(0,0,255)");
        runflage=true;

        if(ui->radioButton_mode->isChecked()){

            if  (ui->radioButton_type1->isChecked()) PMC_MoveCirc(Data::handle,2,axislist,xpoint,ypoint,xpointmid,ypointmid,aspect);
            else if(ui->radioButton_type2->isChecked()) PMC_MoveCirc2(Data::handle,2,axislist,xpointmid,ypointmid,xpoint,ypoint);
            else PMC_MoveCirc3(Data::handle,2,axislist,xpoint,ypoint,radio,aspect);
        }
        else{
            if  (ui->radioButton_type1->isChecked()) PMC_MoveCircAbs(Data::handle,2,axislist,xpoint,ypoint,xpointmid,ypointmid,aspect);
            else if(ui->radioButton_type2->isChecked()) PMC_MoveCirc2Abs(Data::handle,2,axislist,xpointmid,ypointmid,xpoint,ypoint);
            else PMC_MoveCirc3Abs(Data::handle,2,axislist,xpoint,ypoint,radio,aspect);
        }


    }


}

//停止
void MoveCircleForm::on_PushButton_stop_clicked(){
    if(Data::handle!=nullptr){
        PMC_Rapidstop(Data::handle,3);
        ui->pushButton_start->setEnabled(true);
        runflage=false;
        ui->label_flag->setText("停止");
        ui->label_flag->setStyleSheet("background-color: rgb(255,0,0)");
    }

}

//坐标清零
void MoveCircleForm::on_PushButton_cleanpos_clicked(){
    if(Data::handle!=nullptr){
        for(int i=0;i<4;++i){
            PMC_SetDpos(Data::handle,axislist[i],0);
        }
    }
}

//窗口更新
void MoveCircleForm::updateWin(){

    if(Data::handle!=nullptr){
        float dpos=0;
        for(int i=0;i<4;++i){
            PMC_GetDpos(Data::handle,axislist[i],&dpos);
            QString pos = QString::number( dpos,'f',3);
            if(i==0) ui->label_xpos->setText(pos);
            else if(i==1) ui->label_ypos->setText(pos);
            else if(i==2) ui->label_zpos->setText(pos);
            else if(i==3) ui->label_upos->setText(pos);
        }
        if(runflage){
            int value[4]={0,0,0,0};
            for(int i=0;i<4;++i){
                PMC_GetIfIdle(Data::handle,i,&value[i]);
            }
            //cout<<value[0]<<"_"<<value[1]<<"_"<<value[2]<<"_"<<value[3]<<endl;
            if(value[0]==-1&&value[1]==-1&&value[2]==-1&&value[3]==-1){
                runflage=false;
                ui->pushButton_start->setEnabled(true);
                ui->pushButton_start->setEnabled(true);
                ui->label_flag->setText("停止");
                ui->label_flag->setStyleSheet("background-color: rgb(255,0,0)");
            }
        }

    }


}

